package com.rshare.cloud.rspub.trialRegister.ctrl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.rshare.cloud.rspub.common.StringEnhanceUtils;
import com.rshare.cloud.rspub.trialRegister.domain.TrialRegister;
import com.rshare.cloud.rspub.trialRegister.mapper.TrialRegisterMapper;
import com.rshare.cloud.rspub.trialRegister.vo.TrialRegisterVo;


/**
 * 标题、简要说明. <br>
 * 类详细说明.
 * <p>
 * Copyright: Copyright (c) 2017年4月21日 下午1:48:02
 * <p>
 * Company: 苏州罗想软件有限公司
 * <p>
 * @author yi.zhang@roadshare.com
 * @version 1.0.0
 */
@Controller
@RequestMapping(value = "/member")
public class MemberCtrl{

	public static final String SYZC_PREFIX="SYZC";
	
//	@Autowired
//	private PubMgmt pubMgmt;
	
	@Autowired
	private TrialRegisterMapper trialRegisterMapper;
	
	// 短信数字位数
	public final int CODE_LENGTH = 6;
	
	// 短信间隔时间 秒
	public final int MSG_INTERNAL = 120;

	public static final String MSG_CODE_SESSION = "msgCode";


	@RequestMapping(value = "/trialRegister", method = RequestMethod.GET)
	public String getTrialTest()
	{
		return "/test/testMacro";
//		return "hello RSC trial register!";
	}
	

	// 试用注册接口
	// url:${project}/member/trialRegister
	// method:post
	// params:
	// {
	// "RegisterInfo": {
	// "name": "张三",
	// "phone": "13812345678",
	// "email": "admin@admin.com",
	// "corpName": "金刚狼有限公司",
	// "regTime": "2017-04-16 06:00:00",
	// "msgCode":"12345"
	// }
	// }
	// return:
	// String 例如: {"retcode":200,"remsg":"注册成功"} (200:验证成功 300:XXXX字段不符合要求
	// 400:短信验证失败 500:系统失败)

	@RequestMapping(value = "/trialRegister", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> trialRegister(@RequestBody JSONObject jsonObj, HttpSession sess)
	{
//		System.out.println("enter trialRegister post method!");
		Map<String, Object> ret = new HashMap<String, Object>();
		TrialRegisterVo trialRegisterVo=jsonObj.getObject("trialRegisterVo",TrialRegisterVo.class);
		HttpSession session = sess;
		String msgCode = trialRegisterVo.getMsgCode();
		String sessionMsgCode = (String) sess.getAttribute(MemberCtrl.MSG_CODE_SESSION);
		if (StringUtils.isBlank(msgCode) || StringUtils.isBlank(sessionMsgCode) || !StringUtils.equalsIgnoreCase(msgCode, sessionMsgCode))
		{
			ret.put("retcode", 400);
			ret.put("retmsg", "短信验证失败");
			return ret;
		}
		if (StringUtils.isBlank(trialRegisterVo.getName()) || StringUtils.isBlank(trialRegisterVo.getPhone())
		        || StringUtils.isBlank(trialRegisterVo.getCorpName()))
		{
			ret.put("retcode", 300);
			ret.put("retmsg", "字段不符合要求");
			return ret;
		}
		try
		{
			Date now=new Date();
//			设定有效时间为当前时间+30天
	        Calendar c = Calendar.getInstance();

	        c.add(Calendar.DAY_OF_MONTH, 30);
	        Date stopDate=c.getTime();
//	        获取流水号
	        String regId=StringEnhanceUtils.get20SerialNumber(SYZC_PREFIX);
	        TrialRegister trialRegister=new TrialRegister();
	        trialRegister.setRegId(regId);
	        trialRegister.setName(trialRegisterVo.getName());
	        trialRegister.setCorpName(trialRegisterVo.getCorpName());
	        trialRegister.setEmail(trialRegisterVo.getEmail());
	        trialRegister.setPhone(trialRegisterVo.getPhone());
	        trialRegister.setRegTime(now);
	        trialRegister.setAllowTime(now);
	        trialRegister.setStopTime(stopDate);
	        trialRegister.setProbUser("");
	        trialRegister.setProbCropId("");
//	        默认设置为I 申请状态
	        trialRegister.setState("I");
			trialRegisterMapper.AddTrialUser(trialRegister);
			ret.put("retcode", 200);
			ret.put("retmsg", "注册成功");

		}
		catch (Exception ex)
		{
			ex.printStackTrace();
			ret.put("retcode", 500);
			ret.put("retmsg", "系统失败");
		}
		return ret;
	}


/**
 * main函数.
 * @param args
 *            启动参数
 * @throws Exception
 *             Exception
 */
public static void main(String... args) throws Exception
{
//	设定有效时间为当前时间+30天
    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Calendar c = Calendar.getInstance();
    System.out.println("当前日期：              "+sf.format(c.getTime()));
    c.add(Calendar.DAY_OF_MONTH, 30);
    System.out.println("增加30天后日期 ：  "+sf.format(c.getTime()));		
}

}
